<template>
  <div class="user-manage-container">
    <el-card class="header">
      <div class="header-content">
            <div class="search">
                <span>订单鲜花名搜索</span>
                <el-input v-model="flowerName" class="input"></el-input>
                <el-button @click="handleSearch" type="primary">搜索</el-button>
            </div>
             <!-- <el-button type="primary"  @click="onAddUser">{{ $t('msg.excel.addUser') }}</el-button> -->
        </div>
      <div>
      </div>
    </el-card>
    <el-card>
      <el-table v-loading="loading" :data="tableData" style="width: 100%" border>
        <!-- 序列号 -->
        <el-table-column label="#" type="index" align="center" />
        <!-- 商品名称 -->
        <el-table-column :label="$t('msg.excel.flowerName')" prop="flowerName" align="center"/>
         <!-- 商品图片 -->
        <el-table-column :label="$t('msg.excel.orderSrc')" align="center">
          <template #default="{ row }">
            <el-image
              class="avatar"
              :src="row.src"
              :preview-src-list="[row.src]"
            />
          </template>
        </el-table-column>
        <!-- 商品价钱-->
        <el-table-column :label="$t('msg.excel.price')" prop="price" align="center">
          <template #default="{ row }">
            {{ row.price.toFixed(1) }}
          </template>
        </el-table-column>
        <!-- 商品购买数量-->
        <el-table-column :label="$t('msg.excel.salesVolumes')" prop="salesVolumes" align="center" />
         <!-- 购买用户名 -->
         <el-table-column :label="$t('msg.excel.username')" align="center">
          <template #default="{ row }">
            {{ row.userInfo.username }}
          </template>
         </el-table-column>
         <!-- 收货地址 -->
         <el-table-column :label="$t('msg.excel.address')" align="center">
          <template #default="{ row }">
            {{ row.addressInfo?.address || '未填写' }}
          </template>
         </el-table-column>
        <!-- 用户留言-->
        <el-table-column :label="$t('msg.excel.message')" align="center" >
          <template #default="{ row }">
            {{ row.message == "" ? '无' : row.message}}
          </template>
        </el-table-column>
        <!-- 购买时间 -->
        <el-table-column :label="$t('msg.excel.orderTime')" align="center">
          <template #default="{ row }">
            {{ getData(row.orderTime) }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          :label="$t('msg.excel.action')"
          fixed="right"
          width="180"
          align="center"
        >
          <template #default="{ row }">
            <el-button type="primary" size="mini" @click="onShowOrder(row)">{{
              $t('msg.excel.show')
            }}</el-button>
            <!-- <el-button type="danger" size="mini" @click="onDeleteUser(row)">{{
              $t('msg.excel.remove')
            }}</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
  <Dialog
    v-if="dialogFormVisible"
    :dialogVisible = dialogFormVisible
    :type = dialogType
    :orderFormInitial="orderFormInitial"
    @dialogShow="dialogShow"
    @Confirm="handleConfirm"
  />
</template>

<script setup>
import { onMounted, ref, onActivated } from 'vue'
import {getOrdersInfo,updateOrderStatus} from '@/api/commodityOrders'
import { watchSwitchLang } from '@/utils/i18n'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'
import Dialog from './components/Dialog.vue'
// 存储表格数据
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(5)
const router = useRouter()
const i18n = useI18n()
const dialogFormVisible = ref(false)
const dialogType = ref('')
const orderFormInitial = ref()
let loading = ref(false)
// 名称搜索
const flowerName = ref('')
// 调用接口,获取表格数据
const getOrdersList = async () => {
  loading.value = true
  const result = await getOrdersInfo({
    page: page.value,
    pageSize: size.value,
    status:1
  })
  tableData.value = result.findData
  // total.value = result.total
  loading.value = false
}
// page-size改变时触发
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getOrdersList()
}
// current-page 改变时触发
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getOrdersList()
}
// 对时间戳样式进行转换
const getData = (n) =>{
  return dayjs(n).format('YYYY-MM-DD HH:mm:ss')
}
//   点击搜索时的回调函数
const handleSearch = async() =>{
  page.value = 1
     // 调用模糊搜索的接口
  const query = {
    flowerName:flowerName.value,
    page:page.value,
    pageSize:size.value,
    status:1
  }
  const data = await getOrdersInfo(query)
  if(flowerName.value !== '' && data.findData.length === 0){
    ElMessage({
        type: 'error',
        message: '没有此鲜花名相关的订单'
      })
    flowerName.value = ''
    return
  }
  tableData.value = data.findData
  total.value = data.findData.length
  // 修改分页器总数的显示，当输入框中搜索东西的时候，再这样修改
  if(flowerName.value !== ''){
    total.value = data.findData.length
  }else{
    // 没有输入东西的时候，通过接口获取全部数据，再进行赋值
    const getData = await getOrdersInfo({status:1})
    total.value = getData.findData.length
  }
  flowerName.value = ''
}
// // 添加用户操作
// const onAddUser = () =>{
//   dialogFormVisible.value = true
//   dialogType.value = 'add'
// }
// 展示订单信息
const onShowOrder = (row) =>{
  dialogFormVisible.value = true
  orderFormInitial.value = row
}
// 弹框关闭时
const dialogShow =(isShow) =>{
  dialogFormVisible.value = isShow
}
// 子组件点击确定时的回调函数
const handleConfirm = async (value) =>{
    const orderInfo = {
        _id:value._id,
        status:Number(value.status),
        username:value.username
    }
    // 调用创建用户接口
    await updateOrderStatus(orderInfo)
    ElMessage({
      type: 'success',
      message: i18n.t('msg.excel.operationSuccess')
    })
    // 重新获取订单信息
    getOrdersList()
    // 获取全部的数据，返回给total
    const getData = await getOrdersInfo({status:1})
    total.value = getData.findData.length
}
// 删除操作
// const onDeleteUser = (row) => {
//   ElMessageBox.confirm(
//     i18n.t('msg.excel.dialogTitle1') +
//       row.username +
//       i18n.t('msg.excel.dialogTitle2'),
//     '警告',
//     {
//       confirmButtonText: '确定',
//       cancelButtonText: '取消',
//       type: 'warning'
//     }
//   ).then(async () => {
//     await deleteUser({id:row._id})
//     ElMessage({
//       type: 'success',
//       message: i18n.t('msg.excel.removeSuccess')
//     })
//     getOrdersList()
//   })
// }
// 监听语言切换
watchSwitchLang(getOrdersList)
/**
 *  调用时机为首次挂载
 *  以及每次从缓存中被重新插入时
 * 处理导入用户后数据不重新加载的问题
 */
onActivated(getOrdersList)
onMounted(async() => {
  getOrdersList()
  // 获取全部的数据，返回给total
  const getData = await getOrdersInfo({status:1})
  total.value = getData.findData.length
})
</script>

<style lang="scss" scoped>
.user-manage-container {
  .header {
      margin-bottom: 22px;
      text-align: right;
      .header-content{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .search{
            display: flex;
            align-items: center;
            width: 50%;
            .input{
                width: 30%;
                margin: 0 10px;
            }
        }
      }
    }
  :deep(.el-image) {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  :deep(.el-tag) {
    margin-right: 6px;
  }

  .pagination {
    margin-top: 20px;
    text-align: center;
  }
}
</style>
